package com.zhuhjay.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuhjay.boot.mapper.PermissionMapper;
import com.zhuhjay.boot.mapper.RoleMapper;
import com.zhuhjay.boot.mapper.UserMapper;
import com.zhuhjay.domain.entity.Permission;
import com.zhuhjay.domain.entity.Role;
import com.zhuhjay.domain.entity.User;
import com.zhuhjay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuhjay
 * @since 2022-04-02
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public User findAllTableByUsername(String username) {
        // 查询用户信息
        User user = baseMapper.selectOne(
                new QueryWrapper<User>()
                        .eq("username", username));
        if( user != null ){
            // 查询用户的角色信息
            Set<Role> roles = roleMapper.findRolesByUserId(user.getId());
            // 查询角色的权限信息
            roles.forEach(role -> {
                Set<Permission> permissions = permissionMapper
                        .findPermissionsByRoleId(role.getId());
                role.setPermissions(permissions);
            });
            user.setRoles(roles);
        }
        return user;
    }
}
